/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pantallaConsultar.java
 *
 * Created on 06-ago-2010, 15:44:11
 */

package vista.rrhh;

import controlador.rrhh.GestorConsultarTallerCapacitacion;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;
import util.NTupla;
import util.SwingPanel;
import util.TablaUtil;
import vista.*;

/**
 *
 * @author Administrador
 */
public class pantallaConsultarTallerCapacitacion extends javax.swing.JInternalFrame {

    private GestorConsultarTallerCapacitacion gestor;

    /** Creates new form pantallaConsultar */
    public pantallaConsultarTallerCapacitacion() {
        initComponents();

        gestor = new GestorConsultarTallerCapacitacion(this);

        habilitarVentana();
        

    }

    private void habilitarVentana()
    {
        initTabla();

        btnBaja.setEnabled(false);
        btnAlta.setEnabled(false);
    }

    private void setCantidadResultados(int cant)
    {
        lblCantResultados.setText("Cantidad de Resultados: "+cant);
    }

    public void initTabla()
    {
        DefaultTableModel modelo = (DefaultTableModel) tblLista.getModel();

        modelo = TablaUtil.vaciarDefaultTableModel(modelo);

        ArrayList<NTupla> lista = new ArrayList<NTupla>();

        if(optTodo.isSelected())
        {
            lista = gestor.consultarTalleres();
        }
        if(optAlta.isSelected())
        {
            lista = gestor.consultarTalleresEnAlta();
        }
        if(optBaja.isSelected())
        {
            lista = gestor.consultarTalleresEnBaja();
        }

        setCantidadResultados(lista.size());

        Iterator it = lista.iterator();
        while (it.hasNext()) 
        {
            NTupla nt = (NTupla) it.next();

            Object[] fila = new Object[5];
            fila[0] = nt;
                String[] aux = (String[])nt.getData();
                fila[1] = aux[0];
                fila[2] = aux[1];
                fila[3] = aux[2];
                fila[4] = aux[3];

            modelo.addRow(fila);
        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        txtBuscar = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblLista = new javax.swing.JTable();
        btnBaja = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        btnDetalles = new javax.swing.JButton();
        lblCantResultados = new javax.swing.JLabel();
        btnAlta = new javax.swing.JButton();
        optTodo = new javax.swing.JRadioButton();
        optAlta = new javax.swing.JRadioButton();
        optBaja = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Consultar Taller de Capacitación");

        txtBuscar.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        txtBuscar.setForeground(new java.awt.Color(102, 102, 102));
        txtBuscar.setText("Buscar...");
        txtBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtBuscarMouseClicked(evt);
            }
        });
        txtBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBuscarActionPerformed(evt);
            }
        });
        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtBuscarKeyPressed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/search.png"))); // NOI18N

        tblLista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo Taller", "Fecha", "Descripción", "Capacitador", "Estado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblLista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListaMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblListaMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblLista);

        btnBaja.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/delete_page.png"))); // NOI18N
        btnBaja.setText("Dar de Baja");
        btnBaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBajaActionPerformed(evt);
            }
        });

        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/text_page.png"))); // NOI18N
        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/add_page.png"))); // NOI18N
        jButton3.setText("Nuevo");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        btnDetalles.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/search_page.png"))); // NOI18N
        btnDetalles.setText("Ver Detalles");
        btnDetalles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetallesActionPerformed(evt);
            }
        });

        lblCantResultados.setFont(new java.awt.Font("Tahoma", 2, 11));

        btnAlta.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/accept_page.png"))); // NOI18N
        btnAlta.setText("Dar de Alta");
        btnAlta.setEnabled(false);
        btnAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaActionPerformed(evt);
            }
        });

        buttonGroup1.add(optTodo);
        optTodo.setSelected(true);
        optTodo.setText("Todos los Resultados");
        optTodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optTodoActionPerformed(evt);
            }
        });

        buttonGroup1.add(optAlta);
        optAlta.setText("En Alta");
        optAlta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optAltaActionPerformed(evt);
            }
        });

        buttonGroup1.add(optBaja);
        optBaja.setText("En Baja");
        optBaja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optBajaActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Opciones Avanzadas: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 792, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(optTodo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(optAlta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(optBaja)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 214, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblCantResultados, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                                .addGap(133, 133, 133))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(btnDetalles, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAlta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBaja)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(optTodo)
                        .addComponent(optAlta)
                        .addComponent(optBaja)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBaja)
                    .addComponent(lblCantResultados)
                    .addComponent(btnAlta)
                    .addComponent(btnModificar)
                    .addComponent(jButton3)
                    .addComponent(btnDetalles))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtBuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtBuscarMouseClicked

        if(txtBuscar.getText().equals("Buscar..."))
        {
            txtBuscar.setText("");
        }

    }//GEN-LAST:event_txtBuscarMouseClicked

    private void filtrarLista()
    {
            ArrayList<NTupla> lista = new ArrayList<NTupla>();

            if(optTodo.isSelected())
            {
                lista = gestor.buscarTallerCapacitacionPorNombre(txtBuscar.getText());
            }
            if(optAlta.isSelected())
            {
                lista = gestor.buscarTallerCapacitacionPorNombreEnAlta(txtBuscar.getText());
            }
            if(optBaja.isSelected())
            {
                lista = gestor.buscarTallerCapacitacionPorNombreEnBaja(txtBuscar.getText());
            }

            DefaultTableModel modelo = (DefaultTableModel)tblLista.getModel();
            modelo = TablaUtil.vaciarDefaultTableModel(modelo);

            setCantidadResultados(lista.size());

            Iterator<NTupla> it = lista.iterator();
            while (it.hasNext())
            {
                NTupla nt = (NTupla) it.next();

                Object[] fila = new Object[5];
                fila[0] = nt;
                    String[] aux = (String[])nt.getData();
                    fila[1] = aux[0];
                    fila[2] = aux[1];
                    fila[3] = aux[2];
                    fila[4] = aux[3];

                modelo.addRow(fila);
            }
    }

    private void txtBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            filtrarLista();

        }
    }//GEN-LAST:event_txtBuscarKeyPressed

    private void txtBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBuscarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtBuscarActionPerformed

    private void btnDetallesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetallesActionPerformed
       if(tblLista.getSelectedRow()!=-1)
        {
            // Agrego la fila
            DefaultTableModel modelo = (DefaultTableModel) tblLista.getModel();
            NTupla tp = (NTupla) modelo.getValueAt(tblLista.getSelectedRow(),0);

            if(tp.getId()!=0)
            {
                pantallaVerTallerCapacitacion pvtc = new pantallaVerTallerCapacitacion(tp.getId());
                SwingPanel.getInstance().addWindow(pvtc);
                pvtc.setVisible(true);
            }

        }
    }//GEN-LAST:event_btnDetallesActionPerformed

    private void btnBajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBajaActionPerformed

        int seleccion = JOptionPane.showOptionDialog(
                        this, // Componente padre
                        "¿Está seguro que desea dar de BAJA el Taller de Capacitación?", //Mensaje
                        "Confirme su acción", // Título
                        JOptionPane.YES_NO_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,    // null para icono por defecto.
                        new Object[] { "Si", "No" },
                        "Si");

         if (seleccion != -1)
        {
           if((seleccion + 1)==1)
           {
                if(tblLista.getSelectedRow()!=-1)
                {
                    // Agrego la fila
                    DefaultTableModel modelo = (DefaultTableModel) tblLista.getModel();
                    NTupla tp = (NTupla) modelo.getValueAt(tblLista.getSelectedRow(),0);

                    if(tp.getId()!=0)
                    {
                        gestor.darBajaTallerCapacitacion(tp.getId());
                    }

                }
           }
        }



    }//GEN-LAST:event_btnBajaActionPerformed

    private void tblListaMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListaMousePressed

    }//GEN-LAST:event_tblListaMousePressed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed

        pantallaRegistrarTallerCapacitacion prtc = new pantallaRegistrarTallerCapacitacion();
        SwingPanel.getInstance().addWindow(prtc);
        prtc.setVisible(true);

    }//GEN-LAST:event_jButton3ActionPerformed

    private void tblListaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListaMouseClicked
        if(tblLista.getSelectedRow()!=-1)
        {
           DefaultTableModel modelo = (DefaultTableModel) tblLista.getModel();
           NTupla tp = (NTupla) modelo.getValueAt(tblLista.getSelectedRow(),0);
           String[] fila = (String[])tp.getData();
           if(fila[3].equals("Alta"))
           {
                btnBaja.setEnabled(true);
                btnAlta.setEnabled(false);
           }
           else
           {
               btnAlta.setEnabled(true);
               btnBaja.setEnabled(false);
           }
        }

    }//GEN-LAST:event_tblListaMouseClicked

    private void btnAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaActionPerformed

                int seleccion = JOptionPane.showOptionDialog(
                        this, // Componente padre
                        "¿Está seguro que desea dar de ALTA el Taller de Capacitación?", //Mensaje
                        "Confirme su acción", // Título
                        JOptionPane.YES_NO_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null,    // null para icono por defecto.
                        new Object[] { "Si", "No" },
                        "Si");

         if (seleccion != -1)
        {
           if((seleccion + 1)==1)
           {
                if(tblLista.getSelectedRow()!=-1)
                {
                    // Agrego la fila
                    DefaultTableModel modelo = (DefaultTableModel) tblLista.getModel();
                    NTupla tp = (NTupla) modelo.getValueAt(tblLista.getSelectedRow(),0);

                    if(tp.getId()!=0)
                    {
                        gestor.darAltaTallerCapacitacion(tp.getId());
                    }

                }
           }
        }


    }//GEN-LAST:event_btnAltaActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        JOptionPane.showMessageDialog(this.getParent(),"En Construcción","Atencion",JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_btnModificarActionPerformed

    private void optTodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optTodoActionPerformed

        if(!txtBuscar.getText().equals("Buscar..."))
        {
            filtrarLista();
        }
        else
        {
            initTabla();
        }

    }//GEN-LAST:event_optTodoActionPerformed

    private void optAltaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optAltaActionPerformed
        if(!txtBuscar.getText().equals("Buscar..."))
        {
            filtrarLista();
        }
        else
        {
            initTabla();
        }
    }//GEN-LAST:event_optAltaActionPerformed

    private void optBajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optBajaActionPerformed
        if(!txtBuscar.getText().equals("Buscar..."))
        {
            filtrarLista();
        }
        else
        {
            initTabla();
        }
    }//GEN-LAST:event_optBajaActionPerformed


   /**
     * EL-0016 : No se pudo cargar el listado de Talleres de Capacitación
     * ME-0006 : Se dio de baja el taller
     * ME-0007 : Se dio de alta el taller
     * EG-0018 : No se pudo abrir la conexión con la DB para dar de baja el taller
     * @param cod
     */
    public void MostrarMensaje(String cod)
    {
        System.out.println("Se detecto el mensaje: "+cod);
        if(cod.equals("EG-0016"))
        {
            JOptionPane.showMessageDialog(this.getParent(),"No se pudo cargar la lista de Talleres","Error en la Carga",JOptionPane.ERROR_MESSAGE);
        }
        if(cod.equals("EG-0018"))
        {
            JOptionPane.showMessageDialog(this.getParent(),"No se abrir la conexión con ls base de datos para dar de baja el Taller","Error en la Conexión",JOptionPane.ERROR_MESSAGE);
        }
        if(cod.equals("ME-0006"))
        {
            JOptionPane.showMessageDialog(this.getParent(),"Se dió de Baja el Taller de Capacitación","Exito",JOptionPane.INFORMATION_MESSAGE);
        }
        if(cod.equals("ME-0007"))
        {
            JOptionPane.showMessageDialog(this.getParent(),"Se dió de Alta el Taller de Capacitación","Exito",JOptionPane.INFORMATION_MESSAGE);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlta;
    private javax.swing.JButton btnBaja;
    private javax.swing.JButton btnDetalles;
    private javax.swing.JButton btnModificar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCantResultados;
    private javax.swing.JRadioButton optAlta;
    private javax.swing.JRadioButton optBaja;
    private javax.swing.JRadioButton optTodo;
    private javax.swing.JTable tblLista;
    private javax.swing.JTextField txtBuscar;
    // End of variables declaration//GEN-END:variables

}
